package com.jiandian.util;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by melo on 2017/3/30.
 */
public abstract class BaseController {
    protected String OK = "OK";
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        // binder.registerCustomEditor(Date.class, new MyCustomDateEditor());
    }

    private BaseResult baseResult;

    public BaseResult sendResult(int code, String message) {
        return baseResult = new BaseResult(code, message);
    }

    public BaseResult sendResult(int code, String message, Object data) {
        if(data == null){
            if(data instanceof Iterable){
                data = new ArrayList<>();
            }else if(data instanceof String){
                data =  "";
            }else if(data instanceof Integer){
                data =  "";
            }else if(data instanceof Date){
                data =  "";
            }else if(data instanceof Long){
                data =  "";
            }else {
                data =  new HashMap<>();
            }
        }
        return baseResult = new BaseResult(code, message, data);
    }


    public BaseResult getBaseResult() {
        return baseResult;
    }

    public void setBaseResult(BaseResult baseResult) {
        this.baseResult = baseResult;
    }

    public boolean isNotOK(String check) {
        return !OK.equals(check);
    }
}
